package info.niwota.frames;

import java.io.File;
import java.io.FileOutputStream;
import java.io.OutputStream;

import android.content.ContentResolver;
import android.content.ContentValues;
import android.content.Context;
import android.graphics.Bitmap;
import android.graphics.Bitmap.CompressFormat;
import android.location.Location;
import android.net.Uri;
import android.provider.MediaStore.Images;
import android.util.Log;

public class ImageManager {
	private static final Uri STORAGE_URI = Images.Media.EXTERNAL_CONTENT_URI;

	private static final String TAG = "ImageManager";

	//
	// Stores a bitmap or a jpeg byte array to a file (using the specified
	// directory and filename). Also add an entry to the media store for
	// this picture. The title, dateTaken, location are attributes for the
	// picture. The degree is a one element array which returns the orientation
	// of the picture.
	//
	public static Uri addImage(ContentResolver cr, String title,
			long dateTaken, Location location, String directory,
			String filename, Bitmap source) {
		// We should store image data earlier than insert it to ContentProvider,
		// otherwise
		// we may not be able to generate thumbnail in time.
		File dir = new File(directory);
		if (!dir.exists()) {
			dir.mkdirs();
		}
		File file = new File(directory, filename);
		boolean b = saveImage(file, source);
		if (!b) {
			return null; // failed
		}
		//
		ContentValues values = new ContentValues(7);
		values.put(Images.Media.TITLE, title);

		// That filename is what will be handed to Gmail when a user shares a
		// photo. Gmail gets the name of the picture attachment from the
		// "DISPLAY_NAME" field.
		values.put(Images.Media.DISPLAY_NAME, filename);
		values.put(Images.Media.DATE_TAKEN, dateTaken);
		values.put(Images.Media.MIME_TYPE, "image/jpeg");
		values.put(Images.Media.ORIENTATION, 0);
		values.put(Images.Media.DATA, file.getPath());

		if (location != null) {
			values.put(Images.Media.LATITUDE, location.getLatitude());
			values.put(Images.Media.LONGITUDE, location.getLongitude());
		}

		return cr.insert(STORAGE_URI, values);
	}

	public static boolean saveImage(File file, Bitmap source) {
		OutputStream os = null;
		try {
			os = new FileOutputStream(file);
			if (source != null) {
				source.compress(CompressFormat.JPEG, 75, os);
			}
			return true;
		} catch (Exception e) {
			Log.w(TAG, e);
		} finally {
			Util.closeSilently(os);
		}
		return false;
	}

	public static boolean saveImage(Context ctx, String name, int mode, Bitmap source) {
		OutputStream os = null;
		try {
			os = ctx.openFileOutput(name, mode);
			if (source != null) {
				source.compress(CompressFormat.JPEG, 75, os);
			}
			return true;
		} catch (Exception e) {
			Log.w(TAG, e);
		} finally {
			Util.closeSilently(os);
		}
		return false;
	}
	

	public static boolean isMediaScannerScanning(ContentResolver contentResolver) {
		// TODO 
		return false;
	}
}
